package com.eclecticconceptions.vulcan;

import java.text.NumberFormat;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class ImperialRatesActivity extends Activity {

	private int testStatus=0;//0=not running but reset, 1=stopped but not reset
	private Timer imperialTimer;
	public static TextView lblTimer, lblResultGrossKw, lblResultNetKw, lblResultGrossBtu, lblResultNetBtu;
	private static int testTimeSecs = 0;
	
	Spinner spnGasType, spnMeterSize;
	Button btnStart;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.tabimperialrate);
	    lblTimer = (TextView) findViewById(R.id.lblTimer);
	    btnStart=(Button) findViewById(R.id.btnStart);
	    lblResultGrossKw =(TextView) findViewById(R.id.txtResultGrossKw);
	    lblResultNetKw =(TextView) findViewById(R.id.txtResultNetKw);
	    lblResultGrossBtu =(TextView) findViewById(R.id.txtResultGrossBtu);
	    lblResultNetBtu =(TextView) findViewById(R.id.txtResultNetBtu);
	    spnGasType=(Spinner) findViewById(R.id.spnGasType);
	    
	    spnGasType.setOnItemSelectedListener(new GasTypeOnItemSelectedListener());
	    spnMeterSize=(Spinner) findViewById(R.id.spnMeterSizeFt);
	    spnMeterSize.setOnItemSelectedListener(new MeterSizeOnItemSelectedListener());
	    
	}//onCreate

	/*@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  super.onConfigurationChanged(newConfig);
	  setContentView(R.layout.tabimperialrate);
	  lblTimer = (TextView) findViewById(R.id.lblTimer);
	    btnStart=(Button) findViewById(R.id.btnStart);
	    lblResultGrossKw =(TextView) findViewById(R.id.txtResultGrossKw);
	    lblResultNetKw =(TextView) findViewById(R.id.txtResultNetKw);
	    lblResultGrossBtu =(TextView) findViewById(R.id.txtResultGrossBtu);
	    lblResultNetBtu =(TextView) findViewById(R.id.txtResultNetBtu);
	    spnGasType=(Spinner) findViewById(R.id.spnGasType);
	    
	    spnGasType.setOnItemSelectedListener(new GasTypeOnItemSelectedListener());
	    spnMeterSize=(Spinner) findViewById(R.id.spnMeterSizeFt);
	    spnMeterSize.setOnItemSelectedListener(new MeterSizeOnItemSelectedListener());
	    
	}*/
	
	public void onResume(){
		super.onResume();
		spnGasType.setSelection(Cp12Utils.gasCode-1);
	}
	
	public class GasTypeOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	        String selected = parent.getItemAtPosition(pos).toString();
	        //VulcanUtils.errorMsg=selected;
	        if(selected.equals("Natural")) {
	        	Cp12Utils.gasCode=1;
	        	
	        } else if(selected.equals("Propane")) {
	        	Cp12Utils.gasCode=2;
	        	
	        } else if(selected.equals("Butane")){
	        	Cp12Utils.gasCode=3;
	        	
	        } else {
	        	Cp12Utils.gasCode=0;
	        }
	        
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	        // Do nothing.
	    }
	}
	
	public class MeterSizeOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	        String selected = parent.getItemAtPosition(pos).toString();
	        //VulcanUtils.errorMsg=selected;
	        if(selected.equals("U6 (1Cuft/rev)")) {
	        	Cp12Utils.meterSizeFt=1;
	        } else if(selected.equals("U16 (2Cuft/rev)")) {
	        	Cp12Utils.meterSizeFt=2;
	        } else if(selected.equals("U16 (5Cuft/rev)")) {
	        	Cp12Utils.meterSizeFt=5;
	        } else {
	        	Cp12Utils.meterSizeFt=0;
	        }
	        
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	        // Do nothing.
	    }
	}
	
	private void TimerMethod(){
		this.runOnUiThread(Timer_Tick);
	}
	
	private Runnable Timer_Tick = new Runnable(){
		public void run(){
			testTimeSecs++;
			lblTimer.setText(Cp12Utils.formatTime(testTimeSecs));
		}
	};
	
	public void btnStartOnClick(View view){
		//Toast.makeText(this, "meterSizeFt ="+VulcanUtils.meterSizeFt, Toast.LENGTH_SHORT).show();
		//Toast.makeText(this, "gasCode ="+VulcanUtils.gasCode, Toast.LENGTH_SHORT).show();
		switch (testStatus){
		case 0:
			/**if not currently running and reset then start test
			 * start timer
			 * change status
			 * change button text
			 */
			testTimeSecs=0;
	
			imperialTimer = new Timer();
		    
		    btnStart.setText(R.string.Stop);
		    btnStart.setPadding(10, 20, 0, 20);
		    btnStart.setTextSize(40);
		    testStatus=1; //set to running
		    imperialTimer.schedule(new TimerTask(){
		    	@Override
		    	public void run(){
		    		TimerMethod();
		    	}
		    }, 0, 1000);
			break;
		case 1:
			/**else status is 1 and test running
			 * stop timer
			 * call gas rate method and get result
			 * display results
			 * change status to 0
			 * change button text
			 * zero timer display
			 */
			imperialTimer.cancel();
			imperialTimer.purge();
			
			testStatus=2;//stopped but NOT reset
			btnStart.setText(R.string.ResetAll);
			btnStart.setPadding(10, 10, 0, 10);
		    btnStart.setTextSize(35);
			break;
			
		case 2:
			/**stopped but not reset
			 * reset timer and results
			 * change test status
			 * change button text
			 */
			lblTimer.setText(R.string.TimerZero);
			testStatus=0;
			testTimeSecs=0;
			
			btnStart.setText(R.string.Start);
			break;
		}//switch bracket
		
		fillResultsTable(Cp12Utils.getImperialGasRates(testTimeSecs));
	}
	
	public static void fillResultsTable(double kW){
			
			lblResultGrossKw.setText(""+Cp12Utils.round2(kW));
			lblResultNetKw.setText(""+Cp12Utils.round2(kW*0.9009));
			lblResultGrossBtu.setText("("+NumberFormat.getInstance().format((int)(kW*3412))+")");
			lblResultNetBtu.setText("("+NumberFormat.getInstance().format((int)(kW*3412*0.9009))+")");
			
		}
	
}//class bracket
